﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
/////////////////////////////////////
using Microsoft.Maps.MapControl;
using System.Collections.ObjectModel;
using System.Windows.Browser;

using System.Collections.Generic;

namespace BingMapBrowser
{
	public static class Constants
	{
		//Not using these variables.
		public static int MAX_FILTERS = 3;
		public static int MAX_LINKS = 3;
		public static int MAX_LISTITEMS = 4;
	}

	public class MyEntity
	{
		public string EntityName { get; set; }
		public string EntityDisplayName { get; set; }
		public Guid RecordId { get; set; }
		public string FetchXml { get; set; }
		private Address address;
		public Address Address 
		{
			get
			{
				if (address == null)
				{
					address = new Address();
					return address;
				}
				else
				{
					return address;
				}
			}
			set
			{
				address = value;
			}
		}

		private List<Condition> conditions;
		public List<Condition> Conditions
		{
			get
			{
				if (conditions == null)
				{
					conditions = new List<Condition>();
					return conditions;
				}
				else
				{
					return conditions;
				}
			}
			set
			{
				conditions = value;
			}
		}
		private List<Link> links;
		public List<Link> Links 
		{
			get
			{
				if (links == null)
				{
					links = new List<Link>();
					return links;
				}
				else
				{
					return links;
				}
			}
			set
			{
				links = value;
			}
		}

		//Lolz: Inside List is my defined class and I'm not feeling like changing it now. This is why we have Namespaces.
		private List<List> listItems;
		public List<List> ListItems 
		{
			get
			{
				if (listItems == null)
				{
					listItems = new List<List>();
					return listItems;
				}
				else
				{
					return listItems;
				}
			}
			set
			{
				listItems = value;
			}
		}

		private ToolTip toolTip;
		public ToolTip ToolTip 
		{
			get
			{
				if (toolTip == null)
				{
					toolTip = new ToolTip();
					return toolTip;
				}
				else
				{
					return toolTip;
				}
			}
			set
			{
				toolTip = value;
			}
		}

		public void ShowMe()
		{
			try
			{
				MessageBox.Show("Start: " + EntityName);
				MessageBox.Show(Address.Latitude);
				MessageBox.Show(Address.Longitude);

				MessageBox.Show("FILTERS");
				foreach (var condition in Conditions)
				{
					MessageBox.Show(condition.Text);
					MessageBox.Show(condition.Type);
					MessageBox.Show(condition.Expression);
					MessageBox.Show(condition.Value);
				}

				MessageBox.Show("LINKS");
				foreach (var link in Links)
				{
					MessageBox.Show(link.Text);
					MessageBox.Show(link.FieldName);
				}

				MessageBox.Show("LISTITEMS");
				foreach (var listItem in ListItems)
				{
					if (!String.IsNullOrEmpty(listItem.Text))
					{
						MessageBox.Show(listItem.Text);
						MessageBox.Show(listItem.FieldName);
					}
				}

				MessageBox.Show("TOOLTIP");
				MessageBox.Show(ToolTip.Text);
				MessageBox.Show(ToolTip.FieldName);
				MessageBox.Show(ToolTip.Color);
			}
			catch (Exception e)
			{
				MessageBox.Show("Exception Occured in ShowMe: " + e.Message);
			}
		}

		public static MyEntity CreateClone(MyEntity fromEntity)
		{
			if (fromEntity == null)
				return null;

			MyEntity toEntity = new MyEntity();

			toEntity.EntityName = fromEntity.EntityName;
			toEntity.EntityDisplayName = fromEntity.EntityDisplayName;
			toEntity.Address.Latitude = fromEntity.Address.Latitude;
			toEntity.Address.Longitude = fromEntity.Address.Longitude;

			int i = 0;
			foreach (var mapCondition in fromEntity.Conditions)
			{
				toEntity.Conditions.Add(new Condition());
				toEntity.Conditions[i].Expression = mapCondition.Expression;
				toEntity.Conditions[i].Text = mapCondition.Text;
				toEntity.Conditions[i].Type = mapCondition.Type;
				toEntity.Conditions[i].Value = mapCondition.Value;
				i++;
			}

			i = 0;
			foreach (var link in fromEntity.Links)
			{
				toEntity.Links.Add(new Link());
				toEntity.Links[i].FieldName = link.FieldName;
				toEntity.Links[i].Text = link.Text;
				toEntity.Links[i].Value = link.Value;
				i++;
			}

			i = 0;
			foreach (var listItem in fromEntity.ListItems)
			{
				toEntity.ListItems.Add(new List());
				toEntity.ListItems[i].FieldName = listItem.FieldName;
				toEntity.ListItems[i].Text = listItem.Text;
				toEntity.ListItems[i].Value = listItem.Value;
				i++;
			}

			toEntity.ToolTip.FieldName = fromEntity.ToolTip.FieldName;
			toEntity.ToolTip.Text = fromEntity.ToolTip.Text;
			toEntity.ToolTip.Value = fromEntity.ToolTip.Value;
			toEntity.ToolTip.Color = fromEntity.ToolTip.Color;
			toEntity.ToolTip.ZoomLevel = fromEntity.ToolTip.ZoomLevel;
			return toEntity;
		}

		public static MyEntity CreateClone(string entityDisplayName, ObservableCollection<MyEntity> metadata)
		{
			MyEntity fromEntity = null;
			foreach (MyEntity entity in metadata)
			{
				if (entity.EntityDisplayName.Equals(entityDisplayName, StringComparison.OrdinalIgnoreCase))
				{
					fromEntity = entity;
				}
			}
			return CreateClone(fromEntity);
		}

		public void OpenForm()
		{
			string url = GetFormUrl();
			HtmlPage.Window.Navigate(new Uri(url), "_blank", "directories=no,fullscreen=no,menubar=no,resizable=yes,scrollbars=yes,status=no,titlebar=no,toolbar=no,height=800,width=800");
		}

		private string GetFormUrl()
		{
			Uri serverUrl = SilverlightUtility.GetServerBaseUrl();
			string formLink = SilverlightUtility.CombineUrl(serverUrl, "main.aspx?etn={0}&id={1}&pagetype=entityrecord").ToString();
			formLink = String.Format(formLink, EntityName, RecordId);
			return formLink;
		}
	}

	public class Address
	{
		private Location location;
		public Location Location 
		{
			get
			{
				if (location == null)
				{
					location = new Location();
					return location;
				}
				else
				{
					return location;
				}
			}
			set
			{
				location = value;
			}
		}
		public string Latitude { get; set; }
		public string Longitude { get; set; }
	}

	public class Link
	{
		public string Text { get; set; }
		public string FieldName { get; set; }
		public string Value { get; set; }
	}

	public class List
	{
		public string Text { get; set; }
		public string FieldName { get; set; }
		public string Value { get; set; }
	}

	public class ToolTip
	{
		public string Text { get; set; }
		public string FieldName { get; set; }
		public string Value { get; set; }
		public string Color { get; set; }
		public Double ZoomLevel { get; set; }
	}

	public class Condition
	{
		public string Expression { get; set; }
		public string Value { get; set; }
		public string Text { get; set; }
		public string Type { get; set; }
	}

}
